package woodnaonly.radiobutto_test.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import ru.bartwell.exfilepicker.ExFilePickerParcelObject;
import woodnaonly.radiobutto_test.R;

/**
 * Created by woodnaonly on 2015/11/13.
 */
public class ExFilePickerFagment extends Fragment implements View.OnClickListener
{
    private static final int EX_FILE_PICKER_RESULT = 1;
    private Button mButton;
    private ListView mListView;

    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == EX_FILE_PICKER_RESULT)
        {
            if (resultCode == getActivity().RESULT_OK)
            {
                if (data != null)
                {
                    ExFilePickerParcelObject object = (ExFilePickerParcelObject) data.getParcelableExtra(ExFilePickerParcelObject.class.getCanonicalName());
                    if (object.count > 0)
                    {
                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1, object.names);
                        mListView.setAdapter(adapter);
                    }
                }
            }
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.layout3, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        mButton = (Button) view.findViewById(R.id.layout3_button);
        mButton.setOnClickListener(this);
        mListView = (ListView) view.findViewById(R.id.layout3_listview);
    }

    @Override
    public void onClick(View v)
    {
        Intent intent = new Intent(getActivity(), ru.bartwell.exfilepicker.ExFilePickerActivity.class);
        startActivityForResult(intent, EX_FILE_PICKER_RESULT);
    }
}
